package com.yuda.campsite.mapper;

import com.yuda.campsite.domain.InvitationCode;
import com.yuda.campsite.dto.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 邀请码Mapper接口
 *
 * @author wyn
 * @date 2021-09-29
 */
public interface InvitationCodeMapper {
    /**
     * 查询邀请码
     *
     * @param id 邀请码ID
     * @return 邀请码
     */
    /**
     * 查询邀请码列表
     *
     * @param invitationCode 邀请码
     * @return 邀请码集合
     */
    public List<InvitationWithStuInfo> selectInvitationCodeList(InvitationWithStuInfo invitationCode);

    /**
     * 新增邀请码
     *
     * @param invitationCode 邀请码
     * @return 结果
     */
    public int insertInvitationCode(InvitationCode invitationCode);

    void batchInsertInvitationCode(List<InvitationCode> list);

    @Select("select name from invitation_code_classification where campsite_id = #{campsiteId}")
    List<String> campNames(Integer campsiteId);

    @Select("select count(1) from invitation_code_classification  where campsite_id = #{campsiteId} and name = #{name}")
    int invCodeNameisUsed(SimpleInvCodeForGen simpleInvCodeForGen);

    @Select("select id from invitation_code_classification  where campsite_id = #{campsiteId} and name = #{name}")
    int getClassificationId(@Param("name") String name, @Param("campsiteId") Integer campsiteId);


    @Select("select reduction,allNum,orderNum,usedNoOrder,allNum-orderNum-usedNoOrder as leftNum from (\n" +
            "select a.reduction,count(1) allNum, count(c.id) orderNum,count(d.id) as usedNoOrder\n" +
            "from invitation_code_classification a \n" +
            "LEFT JOIN invitation_code b on a.id = b.classifiction_id\n" +
            "LEFT JOIN campsite_order c on c.invitation_code = b.`code` and c.status =6\n" +
            "LEFT JOIN campsite_order d on d.invitation_code = b.`code` and d.status !=6\n" +
            "where a.campsite_id = #{campsiteId} " +
            "GROUP BY a.reduction ) t ")
    List<InvitationCategory> categoryList(Integer campsiteId);

    @Select("select name,reduction,allNum,orderNum,userdNoOrder, (allNum-orderNum-userdNoOrder) leftNum from (\n" +
            "\tselect a.name,a.reduction,count(1) allNum, count(c.id) orderNum,count(d.id) as userdNoOrder\n" +
            "\tfrom invitation_code_classification a \n" +
            "\tLEFT JOIN invitation_code b on a.id = b.classifiction_id\n" +
            "\tLEFT JOIN campsite_order c on c.invitation_code = b.`code` and c.status =6\n" +
            "\tLEFT JOIN campsite_order d on d.invitation_code = b.`code` and d.status !=6\n" +
            "\twhere a.campsite_id = #{campsiteId}\n" +
            "GROUP BY a.name ) t where reduction = #{reduction}")
    List<InvitationCategoryDetail> categoryDetailList(@Param("campsiteId") Integer campsiteId, @Param("reduction") String reduction);


    @Select("select e.name,e.id_card as idCard,e.phone,b.code,b.status as invStatus,c.status as orderStatus\n" +
            "\tfrom invitation_code_classification a \n" +
            "\tLEFT JOIN invitation_code b on a.id = b.classifiction_id\n" +
            "\tLEFT JOIN campsite_order c on c.invitation_code = b.`code`\n" +
            "\tLEFT JOIN student_info e on e.uuid = c.student_id\n" +
            "\twhere a.campsite_id = #{campsiteId} and a.reduction = #{reduction} and a.name = #{name}")
    List<InvCodeWithStuInfo> getInvCodeWithStuInfo(@Param("campsiteId") Integer campsiteId, @Param("reduction") String reduction, @Param("name") String name);
}
